package cn.pping.user.controller;

import cn.pping.basic.exception.BusinessExeption;
import cn.pping.basic.util.JsonResult;
import cn.pping.user.dto.LoginDto;
import cn.pping.user.dto.WxDto;
import cn.pping.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService iLoginService;
    /**
     * 登录
     *
     * @param
     * @return
     */


    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto dto) {
        try {
            // service层, 直接返回一个jsonResult
            //return loginService.account(dto);
            Map<String,Object> map = iLoginService.account(dto);
            // 返回一个map给jsonResult
            return JsonResult.me().setResultObj(map);

        }catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }

    /**
    * @Title: 微信扫码登录
    * @Description: TODO
    * @Author: Pan
    * @Version: 1.0
    * @Date:  2022/8/1 15:00
    * @Parameters: [dto]
    * @Return cn.pping.basic.util.JsonResult
    */

    @GetMapping("/weChat/{code}")
    public JsonResult weChat(@PathVariable("code") String code) {
        try {
            return iLoginService.weChat(code);
        }catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }

    /**
     * @Title: 手机号绑定
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date:  2022/8/1 15:00
     * @Parameters: [dto]
     * @Return cn.pping.basic.util.JsonResult
     */

    @PostMapping("/wechat/binder")
    public JsonResult weChat(@RequestBody WxDto wxdto) {
        try {
            return iLoginService.binder(wxdto);
        }catch (BusinessExeption e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }


}
